<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;

/**
 * Description of GradeController
 *
 * @author gc
 */
class GradeController extends AdminController
{

    public function index()
    {
        $name = I('name');
        $map['status'] = array('egt', 0);
        if (!empty($name)) {
            $map['name'] = array('like', '%' . (string) $name . '%');
        }

        $list = $this->lists('Grade', $map, 'id desc');
        $this->assign('_list', $list);

        $this->meta_title = '班级列表';
        $this->display();
    }

    public function add()
    {
        $id = I("id");
        $cid = I("cid");
        $meta_title = "创建";
        if (!empty($id)) {
            $map["id"] = $id;
            $data = M("Grade")->where($map)->find();
            $data['class_time_cycle'] = date("Y-m-d", $data['start_time']) . ' - ' . date("Y-m-d", $data['end_time']);
            $data['start_time'] = date("Y-m-d", $data['start_time']);

            $obj = json_decode($data['class_time']);
            $week = [];
            foreach ($obj as $v) {
                array_push($week, $v->week);
                $data['hourse_' . $v->week] = $v->hourse;
                $data['minute_' . $v->week] = $v->minute;
            }

            $data['week'] = $week;
            $cid = $data['course_id'];
            $meta_title = "编辑";
            $this->assign("data", $data);
        }
        $course = M("Course")->where(['id' => $cid])->find();
        $this->assign('course', $course);
        $this->meta_title = $meta_title . '《' . $course['title'] . '》课程班级';
        $this->showClassTime();
        $this->display();
    }

    //展示上课频率
    private function showClassTime()
    {
        //时间
        $hourse = [];
        for ($i = 8; $i < 23; $i++) {
            array_push($hourse, $i);
        }
        $minute = ['00', '30'];

        $weekarray = array(1 => "一", 2 => "二", 3 => "三", 4 => "四", 5 => "五", 6 => "六", 7 => "日");

        $this->assign('hourse', $hourse);
        $this->assign('minute', $minute);
        $this->assign('weekarray', $weekarray);
    }

    public function save()
    {
        $post = I("post.");
        $time = explode(' - ', $post['class_time_cycle']);

        $class_time = [];
        foreach ($post['week'] as $v) {
            $temp = [
                'week' => $v,
                'hourse' => $post['hourse_' . $v],
                'minute' => $post['minute_' . $v]
            ];
            array_push($class_time, $temp);
        }

        $data['course_id'] = $post['course_id'];
        $data['name'] = $post['name'];
        $data['have_plan'] = $post['have_plan'] == 1 ? 1 : 0;
        $data['start_time'] = 1 == $data['have_plan'] ? strtotime($post['start_time']) : strtotime(trim($time[0]));
        $data['end_time'] = strtotime(trim($time[1]));
        $data['class_time'] = json_encode($class_time);
        $data['min_num'] = $post['min_num'];
        $data['max_num'] = $post['max_num'];



        if (empty($post['id'])) {
            $data['status'] = 1;
            $data['create_time'] = $data['update_time'] = time();
        } else {
            $data['id'] = $post['id'];
            $data['update_time'] = time();
        }

        $model = M("Grade");
        $s = empty($post['id']) ? $model->add($data) : $model->save($data);

        if (empty($data['end_time']) && $data['have_plan'] == 1 && empty($post['id'])) {
            crtToManyTeach($s);
        }
        
        $s ? $this->success("操作成功！", U("index")) : $this->error("操作失败！");
    }

    //改变课程状态，包括删除
    public function editStatus()
    {
        $ids = I("id");
        $status = I("get.status");
        $map['id'] = array('in', $ids);

        $data = [
            'status' => $status,
            'update_time' => time()
        ];

        $s = M("Grade")->where($map)->save($data);
        $s ? $this->success("操作成功！", U("index")) : $this->error("操作失败！");
    }

    //学员列表
    public function student()
    {
        $grade_id = I("grade_id");

        $grade = M("Grade")->find($grade_id);

        $name = I('name');
        $map['status'] = array('egt', 0);
        if (!empty($name)) {
            $map['name'] = array('like', '%' . (string) $name . '%');
        }
        $map['grade_id'] = $grade_id;
        $map['status'] = 1;

        $list = $this->lists('Grade_student', $map, 'id desc');
        $this->assign('_list', $list);

        $this->meta_title = $grade['name'] . ' 学员列表';
        $this->assign('grade_id', $grade_id);
        $this->display();
    }

    //新增学员
    public function addStudent()
    {
        $id = I("id");
        $meta_title = "新增学员";
        if (!empty($id)) {
            $map['g.id'] = $id;
            $data = M("Grade_student")->alias('g')->field('g.*,s.name')->join('__STUDENT__ s ON g.sid= s.id')->where($map)->find();
            $this->assign("data", $data);
            $meta_title = "编辑学员";
        } else {
            $grade_id = I("grade_id");
            $grade = M("Grade")->find($grade_id);
            if (!empty($grade['start_time'])) {
                $data['start_time'] = $grade['start_time'];
                $data['end_time'] = $grade['have_plan'] == 1 ? $grade['end_time'] : '';
                $this->assign("data", $data);
            }
        }
        //缴费方式
        $this->assign('_pay_method', C("PAY_METHOD"));

        $this->assign('grade_id', $grade_id);
        $this->meta_title = $meta_title;
        $this->display();
    }

    //获取学员列表
    public function ajaxGetStudent()
    {
        $p = I("p");
        $name = I("search_name");
        $map['status'] = array('egt', 0);
        if (!empty($name)) {
            $map['name'] = array('like', '%' . (string) $name . '%');
        }

        //查询
        $grade_id = I("grade_id");
        if (!empty($grade_id)) {
            $where['grade_id'] = $grade_id;
            $where['end_time'] = ['gt', time()];
            $data = M("Grade_student")->field('sid')->where($where)->select();
            $sid_array = [];
            foreach ($data as $v) {
                array_push($sid_array, $v['sid']);
            }
            if (count($sid_array) > 0) {
                $map['id'] = ['not in', $sid_array];
            }
        }

        $list = M("Student")->where($map)->select();
        $this->ajaxReturn($list ? $list : []);
    }

    public function saveStudent()
    {
        $post = I("post.");
        $data['grade_id'] = $post['grade_id'];
        $data['sid'] = $post['student_id'];
        $data['start_time'] = strtotime($post['start_time']);
        $data['end_time'] = strtotime($post['end_time']);
        $data['remark'] = $post['remark'];

        $pay_id = D("ClassPay")->toManyPay($data['sid'], $data['grade_id'], $post['fee'], $post['inputPaymethod']);
        if (!$pay_id) {
            $this->error("操作失败，请稍后再试！");
        }

        $data['pay_id'] = $pay_id;


        if (empty($post['id'])) {
            $data['create_time'] = $data['update_time'] = time();
        } else {
            $data['update_time'] = time();
            $data['id'] = $post['id'];
        }

        $model = M("Grade_student");
        $s = empty($post['id']) ? $model->add($data) : $model->save($data);
        $s ? $this->success("操作成功！", U("student?grade_id=" . $post['grade_id'])) : $this->error("操作失败！");
    }

    //删除学生
    public function delStudent()
    {
        $ids = I("id");
        $map['id'] = array('in', $ids);

        $data = [
            'status' => -1,
            'update_time' => time()
        ];

        $s = M("Grade_student")->where($map)->save($data);
        $s ? $this->success("操作成功！", $_SERVER["HTTP_REFERER"]) : $this->error("操作失败！");
    }

    //延期
    public function delay()
    {
        $id = I("id");
        $data = M("Grade_student")->find($id);
        $this->assign('data', $data);
        $this->meta_title = "课程延期";
        $this->display();
    }

    //保存延期
    public function saveDelay()
    {
        $id = I("id");
        $end_time = I("end_time");
        $data = [
            'end_time' => strtotime($end_time),
            'update_time' => time()
        ];
        $map['id'] = $id;
        $s = M("Grade_student")->where($map)->save($data);
        $grade_id = M("Grade_student")->where($map)->getField('grade_id');
        $s ? $this->success("操作成功！", U("student?grade_id=" . $grade_id)) : $this->error("操作失败！");
    }

}
